package cn.wzvtc.ch4controllerparam;

import org.springframework.web.bind.annotation.*;

@RestController
public class DemoController {

    //接受一般参数
    @GetMapping("/add")
    public Integer add(Integer num1, Integer num2) {
        return num1 + num2;
    }


    @GetMapping("/add2")
    public String add2(String str1, String str2) {
        return str1 + str2;
    }

    /**
     * url中的参数必须要和@RequestParam("num")一致
     *
     * @param name
     * @return
     */
    @GetMapping("/test")
    public String test(@RequestParam(value = "a") String name) {
        return "接收普通参数请求：" + name;
    }

    /**
     * url中没有a参数不会报错、有就显示出来
     *
     * @param name
     * @return
     */
    @GetMapping("/test2")
    public String test2(@RequestParam(value = "a", required = false) String name) {
        return "接收普通参数请求：" + name;
    }

    /**
     * 不输入参数时前端默认a为zs
     *
     * @param name
     * @return
     */
    @GetMapping("/test3")
    public String test3(@RequestParam(value = "a", required = false, defaultValue = "zs") String name) {
        return "接收普通参数请求：" + name;
    }

    /**
     * 接收路径参数
     */
    @GetMapping("/path/{a}")
    public String path(@PathVariable(value = "a") String name) {
        return "接收路径参数:" + name;
    }


    /**
     * post请求
     *
     * @param name
     * @return
     */
    @PostMapping("/testpost")
    public String testpost(@RequestParam(value = "a", required = false, defaultValue = "张三") String name) {
        return "接收普通参数请求：" + name;
    }

    //接收javaBean对象
    @PostMapping("/testjavabean")
    public TestJavabeanParam testjavabean(TestJavabeanParam testJavabeanParam) {
        testJavabeanParam.setStrTemp("OK");
        return testJavabeanParam;
    }

    //接收json对象
    @PostMapping("/testjson")
    public TestJsonParam testjson(@RequestBody TestJsonParam testJsonParam) {
        testJsonParam.setStrTemp("OK");
        return testJsonParam;
    }

}
